package com.zenithmind.common.utils;

import com.zenithmind.common.cache.BloomFilterOperations;
import com.zenithmind.common.cache.CacheStrategy;
import com.zenithmind.common.cache.DistributedLockOperations;
import com.zenithmind.common.cache.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;

/**
 * Redis工具类 - 重构后的版本
 * 遵循单一职责原则：作为各种Redis操作的门面类
 * 遵循依赖倒置原则：依赖抽象接口而非具体实现
 * 遵循迪米特法则：隐藏内部实现细节，只暴露必要的接口
 *
 * @deprecated 建议直接使用具体的操作接口：
 * - {@link RedisOperations} 基础Redis操作
 * - {@link BloomFilterOperations} 布隆过滤器操作
 * - {@link DistributedLockOperations} 分布式锁操作
 * - {@link CacheStrategy} 缓存策略操作
 */
@Component
public class RedisUtils {

    @Autowired
    private RedisOperations redisOperations;

    @Autowired
    private BloomFilterOperations bloomFilterOperations;

    @Autowired
    private DistributedLockOperations distributedLockOperations;

    @Autowired
    private CacheStrategy cacheStrategy;

    // ================ 基础操作 - 委托给RedisOperations ================

    /**
     * 设置缓存
     * @deprecated 请使用 {@link RedisOperations#set(String, Object)}
     */
    @Deprecated
    public void set(String key, Object value) {
        redisOperations.set(key, value);
    }

    /**
     * 设置缓存并指定过期时间
     * @deprecated 请使用 {@link RedisOperations#set(String, Object, long)}
     */
    @Deprecated
    public void set(String key, Object value, long expire) {
        redisOperations.set(key, value, expire);
    }

    /**
     * 获取缓存
     * @deprecated 请使用 {@link RedisOperations#get(String)}
     */
    @Deprecated
    public Object get(String key) {
        return redisOperations.get(key);
    }

    /**
     * 删除缓存
     * @deprecated 请使用 {@link RedisOperations#delete(String)}
     */
    @Deprecated
    public boolean delete(String key) {
        return redisOperations.delete(key);
    }

    /**
     * 批量删除缓存
     * @deprecated 请使用 {@link RedisOperations#delete(List)}
     */
    @Deprecated
    public void delete(List<String> keys) {
        redisOperations.delete(keys);
    }

    /**
     * 设置过期时间
     * @deprecated 请使用 {@link RedisOperations#expire(String, long)}
     */
    @Deprecated
    public boolean expire(String key, long expire) {
        return redisOperations.expire(key, expire);
    }

    /**
     * 获取过期时间
     * @deprecated 请使用 {@link RedisOperations#getExpire(String)}
     */
    @Deprecated
    public long getExpire(String key) {
        return redisOperations.getExpire(key);
    }

    /**
     * 判断key是否存在
     * @deprecated 请使用 {@link RedisOperations#hasKey(String)}
     */
    @Deprecated
    public boolean hasKey(String key) {
        return redisOperations.hasKey(key);
    }

    /**
     * 增加值
     * @deprecated 请使用 {@link RedisOperations#incr(String, long)}
     */
    @Deprecated
    public long incr(String key, long delta) {
        return redisOperations.incr(key, delta);
    }

    /**
     * 减少值
     * @deprecated 请使用 {@link RedisOperations#decr(String, long)}
     */
    @Deprecated
    public long decr(String key, long delta) {
        return redisOperations.decr(key, delta);
    }

    // ================ Hash操作 - 委托给RedisOperations ================

    /**
     * Hash操作 - 设置值
     * @deprecated 请使用 {@link RedisOperations#hSet(String, String, Object)}
     */
    @Deprecated
    public void hSet(String key, String hashKey, Object value) {
        redisOperations.hSet(key, hashKey, value);
    }

    /**
     * Hash操作 - 设置值并指定过期时间
     * @deprecated 请使用 {@link RedisOperations#hSet(String, String, Object, long)}
     */
    @Deprecated
    public void hSet(String key, String hashKey, Object value, long expire) {
        redisOperations.hSet(key, hashKey, value, expire);
    }

    /**
     * Hash操作 - 获取值
     * @deprecated 请使用 {@link RedisOperations#hGet(String, String)}
     */
    @Deprecated
    public Object hGet(String key, String hashKey) {
        return redisOperations.hGet(key, hashKey);
    }

    /**
     * Hash操作 - 批量设置值
     * @deprecated 请使用 {@link RedisOperations#hSetAll(String, Map)}
     */
    @Deprecated
    public void hSetAll(String key, Map<String, Object> map) {
        redisOperations.hSetAll(key, map);
    }

    /**
     * Hash操作 - 批量设置值并指定过期时间
     * @deprecated 请使用 {@link RedisOperations#hSetAll(String, Map, long)}
     */
    @Deprecated
    public void hSetAll(String key, Map<String, Object> map, long expire) {
        redisOperations.hSetAll(key, map, expire);
    }

    /**
     * Hash操作 - 获取所有值
     * @deprecated 请使用 {@link RedisOperations#hGetAll(String)}
     */
    @Deprecated
    public Map<Object, Object> hGetAll(String key) {
        return redisOperations.hGetAll(key);
    }

    /**
     * Hash操作 - 删除值
     * @deprecated 请使用 {@link RedisOperations#hDelete(String, Object...)}
     */
    @Deprecated
    public void hDelete(String key, Object... hashKeys) {
        redisOperations.hDelete(key, hashKeys);
    }

    /**
     * Hash操作 - 判断是否有该值
     * @deprecated 请使用 {@link RedisOperations#hHasKey(String, String)}
     */
    @Deprecated
    public boolean hHasKey(String key, String hashKey) {
        return redisOperations.hHasKey(key, hashKey);
    }

    /**
     * Hash操作 - 增加值
     * @deprecated 请使用 {@link RedisOperations#hIncr(String, String, long)}
     */
    @Deprecated
    public long hIncr(String key, String hashKey, long delta) {
        return redisOperations.hIncr(key, hashKey, delta);
    }

    /**
     * Hash操作 - 减少值
     * @deprecated 请使用 {@link RedisOperations#hDecr(String, String, long)}
     */
    @Deprecated
    public long hDecr(String key, String hashKey, long delta) {
        return redisOperations.hDecr(key, hashKey, delta);
    }

    // ================ Set操作 - 委托给RedisOperations ================

    /**
     * Set操作 - 添加值
     * @deprecated 请使用 {@link RedisOperations#sAdd(String, Object...)}
     */
    @Deprecated
    public void sAdd(String key, Object... value) {
        redisOperations.sAdd(key, value);
    }

    /**
     * Set操作 - 获取所有值
     * @deprecated 请使用 {@link RedisOperations#sMembers(String)}
     */
    @Deprecated
    public Set<Object> sMembers(String key) {
        return redisOperations.sMembers(key);
    }

    /**
     * Set操作 - 判断是否有该值
     * @deprecated 请使用 {@link RedisOperations#sIsMember(String, Object)}
     */
    @Deprecated
    public boolean sIsMember(String key, Object value) {
        return redisOperations.sIsMember(key, value);
    }

    /**
     * Set操作 - 获取集合大小
     * @deprecated 请使用 {@link RedisOperations#sSize(String)}
     */
    @Deprecated
    public long sSize(String key) {
        return redisOperations.sSize(key);
    }

    /**
     * Set操作 - 删除值
     * @deprecated 请使用 {@link RedisOperations#sRemove(String, Object...)}
     */
    @Deprecated
    public long sRemove(String key, Object... value) {
        return redisOperations.sRemove(key, value);
    }

    // ================ List操作 - 委托给RedisOperations ================

    /**
     * List操作 - 添加值（从左边添加）
     * @deprecated 请使用 {@link RedisOperations#lLeftPush(String, Object)}
     */
    @Deprecated
    public void lLeftPush(String key, Object value) {
        redisOperations.lLeftPush(key, value);
    }

    /**
     * List操作 - 添加值（从右边添加）
     * @deprecated 请使用 {@link RedisOperations#lRightPush(String, Object)}
     */
    @Deprecated
    public void lRightPush(String key, Object value) {
        redisOperations.lRightPush(key, value);
    }

    /**
     * List操作 - 获取指定范围的值
     * @deprecated 请使用 {@link RedisOperations#lRange(String, long, long)}
     */
    @Deprecated
    public List<Object> lRange(String key, long start, long end) {
        return redisOperations.lRange(key, start, end);
    }

    /**
     * List操作 - 获取列表长度
     * @deprecated 请使用 {@link RedisOperations#lSize(String)}
     */
    @Deprecated
    public long lSize(String key) {
        return redisOperations.lSize(key);
    }

    /**
     * List操作 - 获取指定位置的值
     * @deprecated 请使用 {@link RedisOperations#lIndex(String, long)}
     */
    @Deprecated
    public Object lIndex(String key, long index) {
        return redisOperations.lIndex(key, index);
    }

    /**
     * List操作 - 删除值
     * @deprecated 请使用 {@link RedisOperations#lRemove(String, long, Object)}
     */
    @Deprecated
    public long lRemove(String key, long count, Object value) {
        return redisOperations.lRemove(key, count, value);
    }

    // ================ 布隆过滤器（防止缓存穿透） - 委托给BloomFilterOperations ================

    /**
     * 初始化布隆过滤器
     * @deprecated 请使用 {@link BloomFilterOperations#initBloomFilter(String, long, double)}
     */
    @Deprecated
    public <T> org.redisson.api.RBloomFilter<T> initBloomFilter(String filterName, long expectedSize, double falseProbability) {
        return bloomFilterOperations.initBloomFilter(filterName, expectedSize, falseProbability);
    }

    /**
     * 向布隆过滤器中添加元素
     * @deprecated 请使用 {@link BloomFilterOperations#addToBloomFilter(String, Object)}
     */
    @Deprecated
    public <T> boolean addToBloomFilter(String filterName, T value) {
        return bloomFilterOperations.addToBloomFilter(filterName, value);
    }

    /**
     * 判断元素是否在布隆过滤器中
     * @deprecated 请使用 {@link BloomFilterOperations#isInBloomFilter(String, Object)}
     */
    @Deprecated
    public <T> boolean isInBloomFilter(String filterName, T value) {
        return bloomFilterOperations.isInBloomFilter(filterName, value);
    }

    // ================ 分布式锁（防止缓存击穿） - 委托给DistributedLockOperations ================

    /**
     * 获取分布式锁
     * @deprecated 请使用 {@link DistributedLockOperations#getLock(String)}
     */
    @Deprecated
    public org.redisson.api.RLock getLock(String lockName) {
        return distributedLockOperations.getLock(lockName);
    }

    /**
     * 尝试获取分布式锁
     * @deprecated 请使用 {@link DistributedLockOperations#tryLock(String, long, long, TimeUnit)}
     */
    @Deprecated
    public boolean tryLock(String lockName, long waitTime, long leaseTime, TimeUnit timeUnit) {
        return distributedLockOperations.tryLock(lockName, waitTime, leaseTime, timeUnit);
    }

    /**
     * 释放分布式锁
     * @deprecated 请使用 {@link DistributedLockOperations#unlock(String)}
     */
    @Deprecated
    public void unlock(String lockName) {
        distributedLockOperations.unlock(lockName);
    }

    // ================ 缓存策略 - 委托给CacheStrategy ================

    /**
     * 防止缓存穿透的查询方法
     * @deprecated 请使用 {@link CacheStrategy#getWithPenetrationPrevention(String, Supplier, long)}
     */
    @Deprecated
    public <T> T getWithPenetrationPrevention(String key, Supplier<T> supplier, long expire) {
        return cacheStrategy.getWithPenetrationPrevention(key, supplier, expire);
    }

    /**
     * 防止缓存击穿的查询方法
     * @deprecated 请使用 {@link CacheStrategy#getWithBreakdownPrevention(String, String, Supplier, long)}
     */
    @Deprecated
    public <T> T getWithBreakdownPrevention(String key, String lockKey, Supplier<T> supplier, long expire) {
        return cacheStrategy.getWithBreakdownPrevention(key, lockKey, supplier, expire);
    }

    /**
     * 更新缓存（先更新数据库，再删除缓存）
     * @deprecated 请使用 {@link CacheStrategy#update(String, String, Runnable)}
     */
    @Deprecated
    public void update(String key, String lockKey, Runnable updateOp) {
        cacheStrategy.update(key, lockKey, updateOp);
    }


} 